package com.ftrend.printdemo.mhttemplate;

import android.content.Context;
import android.graphics.Typeface;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;


import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;

import java.util.ArrayList;
import java.util.List;

public class MTHPrintBarCodeUtils {
    private static String unit = "￥";
    private final Context mContext;
    private static Typeface typeface = null;
    private int fontSize0 = 0;
    private int fontSize1 = 0;
    private int fontSize2 = 0;

    private int ptintWidth = 40;
    private int ptintHeigh = 30;

    private MTHPrintBarCodeUtils(Context mContext) {
        this.mContext = mContext;
    }

    public static MTHPrintBarCodeUtils of(Context mContext) {
        return new MTHPrintBarCodeUtils(mContext);
    }

    /**
     * 发送标签
     *
     * @return
     */
    public List<PrinterPaper> sendLabel() throws WriterException {


        // TODO: 检查打印机状态
//        if(PrinterBarUsb.isBarConnect()){
//            try {
//                int status =  PrinterBarUsb.getPrinterState(PrinterBarUsb.getPortManager().getCommand(),100);
//                CrashManager.getInstance().printerMsg("PrinterBarUsb  MTH-L58G status=="+status);
//                if (status == -3||status ==-1) {//打印机处于不正常状态,则不发送打印任务
//                    Message msg = new Message();
//                    msg.what = 0x01;
//                    msg.arg1 = -1;
//                    handler.sendMessage(msg);
//                    return null;
//                }
//            } catch (IOException e) {
//                Log.e("IOException",e.toString());
//                e.printStackTrace();
//            }
//        }else  if (PrinterBlue.isBarConnect()) {
//            try {
//                Command command = PrinterBlue.getPortManager().getCommand();
//                int status = PrinterBlue.getPrinterState(command,100);
//                CrashManager.getInstance().printerMsg("PrinterBlue MTH-L58G status=="+status);
//                if (status == -3||status ==-1) {//打印机处于不正常状态,则不发送打印任务
//                    Message msg = new Message();
//                    msg.what = 0x01;
//                    msg.arg1 = -1;
//                    handler.sendMessage(msg);
//                    return null;
//                }
//            } catch (IOException e) {
//                e.printStackTrace();
//                CrashManager.getInstance().saveCatchInfo2File(e);
//            }
//        }


        if (ptintHeigh == 30 && ptintWidth == 40) {
            return sendLabel4030("衣点通");
        }
        return null;
    }

    /**
     * 发送标签40*30
     *
     * @return
     */
    public List<PrinterPaper> sendLabel4030(String storeName) throws WriterException {
        int lineSpacing = 5;
        List<PrinterPaper> papers = new ArrayList<>();
        PrinterPaper paper = new PrinterPaper(mContext, new Size(ptintWidth, ptintHeigh), new Margin(0, 0, 6, 16), lineSpacing);
        paper.addLineText(storeName, new Style(Style.Align.LEFT),typeface);
        BarCodeElement codeElement = new BarCodeElement("12345454", BarcodeFormat.CODE_128);
        paper.addBarCode(codeElement, new Style(Style.Align.CENTER));
        paper.addLineText("12345454", new Style(Style.Align.CENTER, 6),typeface);
        paper.setPrintNum(1);
        papers.add(paper);
        return papers;
    }


    public static PrinterPaper handleTscText(String content, PrinterPaper paper, int warpLength, String strPrefix){
        int len = calcWarpLength(content,warpLength);//可以打印七个中文字符宽度
        if(len>0) {
            String str = "";
            if (content.length() < len) {
                str = content;
            } else {
                str = content.substring(0, len);
            }
            paper.addLineText(strPrefix + "：" + str, new Style(Style.Align.LEFT),typeface);
            if (content.length() > len) {
                paper.addLineText(content.substring(len, content.length()), new Style(Style.Align.LEFT),typeface);
            }
        }else {
            paper.addLineText(strPrefix + "：" + content, new Style(Style.Align.LEFT),typeface);
        }
        return paper;
    }

    public static int calcWarpLength(String str, int length) {
        if (str == null || str.length() == 0) {
            return 0;
        }
        StringBuilder sb = new StringBuilder();
        double i = 0;
        for (char c : str.toCharArray()) {
            sb.append(c);
            if(c<128){
                i=i+0.5;
            }else {
                i=i+1;
            }
            if(i>=length){
                break;
            }
        }
        int leng = 0;
        if(i==length){
            leng =sb.toString().length();
        }else if(i>length){
            leng =sb.toString().length()-1;
        }
        return leng;
    }


    /**
     * 获取打印行间距
     *
     * @param printHeight         纸张高度
     * @param lableValueAttribute 属性设置
     * @return 行间距
     */
    private int getPrintLineSpacing(int printHeight, String[] lableValueAttribute,int heightSpace) {
        int indexNum = 0;
        for (String attr : lableValueAttribute) {
            if (!attr.contains("-")) {
                indexNum += 1;
            }
        }
        if (indexNum > 7) {
            return 10;
        } else {
            return heightSpace;
        }
    }
}
